﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid;
using DevExpress.Utils;

using DevExpress.XtraEditors.Repository;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;

using MM.Entities;
using MM.Business; 

namespace MM
{
    public partial class frmBaoCaoNXT_Tonghop : Form
    {
        List<VAT_TU_PHU_TUNG> lstVTPT = new List<VAT_TU_PHU_TUNG>();
        string _matau = "";
        string _nguoitao = "";
        string _sConn = "";
        string _plant = "";

        List<TON_KHO> lstTonKho = new List<TON_KHO>();
        frmTimKiemVTPT frm;
        frmTimKiemTheoHinh frm1;
        frmImpa_TimKiem frm2;
        frmImpa_TimKiemTheoHinh frm3;

        string[] arrThamSo = new string[10];

        public frmBaoCaoNXT_Tonghop(string sConn)
        {
            InitializeComponent();

            _sConn = sConn;
            _matau = frmDangNhap.objTau._tenTau2KiTu;
            _plant = frmDangNhap.objTau._plantReferenceSAP;

            this.popupContainerEdit1.EditValue = "Chọn xem tồn kho";
            this.dateEditFromDate.DateTime = DateTime.Today;
            this.dateEditToDate.DateTime = DateTime.Today;

            this.popupContainerEdit1.Properties.CloseOnOuterMouseClick = false;

            this.gridControl1.DataSource = lstTonKho;

         

            this.InitDMKho(_matau);
            this.InitTrangthai();
            this.InitNhomVTPT();

            this.InitThamSo();
        }

        public void InitThamSo()
        {
            THAM_SO objTS = new THAM_SO();
            clsTHAM_SO tsCtrl = new clsTHAM_SO(_sConn);
            objTS = tsCtrl.SelectTHAM_SO("vThuocTinh");
            if (objTS != null && objTS.GiaTri != "")
            {
                arrThamSo = objTS.GiaTri.Split(',');
            }
        }
        private void InitDMKho(string matau)
        {
            clsKHO khoCtrl = new clsKHO(_sConn);
            List<KHO> lstKho = new List<KHO>();
            lstKho = khoCtrl.LoadKHOByTAU(matau);
            this.lookUpKho.Properties.DataSource = lstKho;
            this.lookUpKho.Properties.DisplayMember = "MaKho";
            this.lookUpKho.Properties.ValueMember = "MaKho";
        }
        private void InitTrangthai()
        {
            clsTRANG_THAI_VTPT ttCtrl = new clsTRANG_THAI_VTPT(_sConn);
            List<TRANG_THAI_VTPT> lst = new List<TRANG_THAI_VTPT>();
            lst = ttCtrl.usp_SelectTRANG_THAI_VTPTsAll();
            this.lookUpTrangthai.Properties.DataSource = lst;
            this.lookUpTrangthai.Properties.DisplayMember = "TenTrangThai";
            this.lookUpTrangthai.Properties.ValueMember = "MaTrangThai";
            if (lst.Count > 0)
            {
                this.lookUpTrangthai.Properties.ForceInitialize();
                this.lookUpTrangthai.ItemIndex = 0;
            }
        }
        private void InitNhomVTPT()
        {
            clsNHOM_VTPT nhomCtrl = new clsNHOM_VTPT(_sConn);
            List<NHOM_VTPT> lst = new List<NHOM_VTPT>();
            lst = nhomCtrl.usp_SelectNHOM_VTPT();
            this.lookUpNhomVTPT.Properties.DataSource = lst;
            this.lookUpNhomVTPT.Properties.DisplayMember = "TenNhomVTPT";
            this.lookUpNhomVTPT.Properties.ValueMember = "MaNhomVTPT";
        }
        private void cmdXem_Click(object sender, EventArgs e)
        {
            try
            {
                

                this.popupContainerEdit1.ClosePopup();

                StringBuilder s = new StringBuilder();
                if (this.lookUpKho.Text!="")
                    s.Append("KHO: " + this.lookUpKho.Text);
                if (this.lookUpTrangthai.Text != "")
                    s.Append(" # TRẠNG THÁI: " + this.lookUpTrangthai.Text);
                if (this.dateEditFromDate.Text == "")
                    this.dateEditFromDate.DateTime = Convert.ToDateTime("01/01/2012");
                if (this.dateEditToDate.Text == "")
                    this.dateEditToDate.DateTime = DateTime.Today;
                s.Append(" # TỪ NGÀY: " + this.dateEditFromDate.Text);
                s.Append(" ĐẾN NGÀY: " + this.dateEditToDate.Text);
                if (this.lookUpNhomVTPT.Text != "")
                    s.Append(" # NHÓM VTPT: " + this.lookUpNhomVTPT.Text);

                this.popupContainerEdit1.EditValue = s.ToString();

                this.GetTonKhoTongHop();
                
            }
            catch { }
        }

        private void lnkSoPhieuNK_Click(object sender, EventArgs e)
        {
            ColumnView col = (ColumnView)this.gridControl1.FocusedView;
            try
            {
                int index = this.gridDSVT.FocusedRowHandle;

                TON_KHO obj = new TON_KHO();

                obj.MaKho = this.gridDSVT.GetRowCellValue(index, "MaKho").ToString();
                obj.MaKhoFrom = this.gridDSVT.GetRowCellValue(index, "MaKhoThamChieuSAP").ToString();
                obj.MaTrangThai = int.Parse(this.gridDSVT.GetRowCellValue(index, "MaTrangThai").ToString());
                obj.FromDate = this.dateEditFromDate.DateTime;
                obj.ToDate = this.dateEditToDate.DateTime;
                obj.DauKy = int.Parse(this.gridDSVT.GetRowCellValue(index, "DauKy").ToString());
                obj.TenKho = this.gridDSVT.GetRowCellValue(index, "TenKho").ToString();
                obj.TenTrangThai = this.gridDSVT.GetRowCellValue(index, "TenTrangThai").ToString();
                obj.TenVTPT = this.gridDSVT.GetRowCellValue(index, "TenVTPT").ToString();
                obj.MaVTPTFrom = this.gridDSVT.GetRowCellValue(index, "MaVTPT").ToString();
                obj.MaVTPT = this.gridDSVT.GetRowCellValue(index, "MaVTPT").ToString();
                obj.PlantFrom = _plant;

                frmBaoCaoNXT_Chitiet f = new frmBaoCaoNXT_Chitiet(_sConn, obj);
                f.ShowDialog();
            }
            catch { }
        }

        private void frmBaoCaoNXT_Tonghop_Paint(object sender, PaintEventArgs e)
        {
            //Common.PaintGradientColor(Color.LightYellow, Color.LightSkyBlue, System.Drawing.Drawing2D.LinearGradientMode.Vertical, this.Width, this.Height, this);            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstTonKho.Count > 0)
                {
                    string filepath = "";
                    FolderBrowserDialog dg = new FolderBrowserDialog();
                    dg.ShowDialog();
                    if (dg.SelectedPath != "")
                    {
                        TON_KHO tk = new TON_KHO();

                        string makho = "";
                        string nhomvtpt = "";
                        int matrangthai = 0;
                        DateTime fDate;
                        DateTime tDate;

                        fDate = this.dateEditFromDate.DateTime;
                        tDate = this.dateEditToDate.DateTime;
                        makho = this.lookUpKho.Text == "" ? "" : this.lookUpKho.GetColumnValue("MaKhoThamChieuSAP").ToString();
                        matrangthai = this.lookUpTrangthai.Text == "" ? -1 : int.Parse(this.lookUpTrangthai.GetColumnValue("MaTrangThai").ToString());

                        tk.FromDate = fDate;
                        tk.ToDate = tDate;
                        tk.MaKhoFrom = makho;
                        tk.PlantFrom = _plant;
                        tk.MaVTPTFrom = this.txtMaVTPT.Text;
                        tk.NhomVTPTFrom = this.lookUpNhomVTPT.Text;

                        clsTON_KHO tkCtrl = new clsTON_KHO(_sConn);
                        // xuat file Csv
                        string y = DateTime.Today.Year.ToString();
                        string m = DateTime.Today.Month.ToString();
                        string d = DateTime.Today.Day.ToString();
                        m = Convert.ToString("0" + m).Substring(m.Length - 1, 2);
                        d = Convert.ToString("0" + d).Substring(d.Length - 1, 2);
                        filepath = dg.SelectedPath + "\\" + "IB" + _matau + "_" + y + m + d + ".csv";
                        tkCtrl.CsvExportTON_KHO_Tonghop(tk, lstTonKho, filepath);
                        MessageBox.Show("XUẤT FILE THÀNH CÔNG!" + "\n" + "GỬI EMAIL ĐÍNH KÈM FILE TẠI: " + filepath + " VỀ ĐỊA CHỈ: vmm@vitranschart.com.vn");
                    }

                }
                else
                    MessageBox.Show("Không có dữ liệu để xuất");
            }
            catch { }
        }

        private void cmdTimVTPT_Click(object sender, EventArgs e)
        {
            lstVTPT.Clear();
            frm.SelectLstMaterial += new frmTimKiemVTPT.SelectListMaterialHandler(LuuDSVTPT);
            frm.ShowDialog();
        }
        private void LuuDSVTPT(object sender, lstVtptEventArgs e)
        {
            lstVTPT = e.lstVtpt;
            if (lstVTPT.Count > 0)
            {
                this.txtMaVTPT.Text = lstVTPT[0]._maVTPT;                
            }
        }

        private void frmBaoCaoNXT_Tonghop_Load(object sender, EventArgs e)
        {
            Common.SetColor(this);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
               

                this.popupContainerEdit1.ClosePopup();

                StringBuilder s = new StringBuilder();
                if (this.lookUpKho.Text != "")
                    s.Append("KHO: " + this.lookUpKho.Text);
                if (this.lookUpTrangthai.Text != "")
                    s.Append(" # TRẠNG THÁI: " + this.lookUpTrangthai.Text);
                if (this.dateEditFromDate.Text == "")
                    this.dateEditFromDate.DateTime = Convert.ToDateTime("01/01/2012");
                if (this.dateEditToDate.Text == "")
                    this.dateEditToDate.DateTime = DateTime.Today;
                s.Append(" # TỪ NGÀY: " + this.dateEditFromDate.Text);
                s.Append(" ĐẾN NGÀY: " + this.dateEditToDate.Text);

                this.popupContainerEdit1.EditValue = s.ToString();

                this.GetTonKhoTongHop();

                DateTime fDate;
                DateTime tDate;

                fDate = this.dateEditFromDate.DateTime;
                tDate = this.dateEditToDate.DateTime;
               
                List<TON_KHO> lstTonKhoCT = new List<TON_KHO>();
               
              

                clsTON_KHO tkCtrl = new clsTON_KHO(_sConn);
                TON_KHO obj = new TON_KHO();
                obj.FromDate = fDate;
                obj.ToDate = tDate;
                for (int i = 0; i < lstTonKho.Count; i++)
                {
                    obj.MaKho = lstTonKho[i].MaKho;
                    obj.MaVTPT = lstTonKho[i].MaVTPT;
                    obj.MaTrangThai = lstTonKho[i].MaTrangThai;
                    List<TON_KHO> lsttemp = new List<TON_KHO>();
                    lsttemp = tkCtrl.usp_XemTON_KHO_Chitiet(obj);
                    for (int j = 0; j < lsttemp.Count; j++)
                    {                      

                        lstTonKhoCT.Add(lsttemp[j]);
                    }
                }

                // xử lý lstTonKhoCT : loại bỏ các phiếu NK,XK,CK bị hủy
                int count = lstTonKhoCT.Count;
                List<TON_KHO> lstTonKhoCT_Huy = new List<TON_KHO>();
                for (int i = 0; i < count; i++)
                {
                    if (lstTonKhoCT[i].MaGiaoTacKho.Contains("HUY"))
                    {
                        lstTonKhoCT_Huy.Add(lstTonKhoCT[i]);
                        lstTonKhoCT[i].MaTonKho=-1;
                    }
                }

                lstTonKhoCT.RemoveAll(delegate(TON_KHO x) {return x.MaTonKho==-1;});
             
                for (int j = 0; j < lstTonKhoCT_Huy.Count; j++)
                {
                    lstTonKhoCT.RemoveAll(delegate(TON_KHO x) { return x.MaChiTiet==lstTonKhoCT_Huy[j].MaChiTiet && x.MaVTPT==lstTonKhoCT_Huy[j].MaVTPT && x.MaKho==lstTonKhoCT_Huy[j].MaKho && x.MaTrangThai==lstTonKhoCT_Huy[j].MaTrangThai && x.SoPhieuGoc==lstTonKhoCT_Huy[j].SoPhieuGoc; });
                }



                // REPORT
                //rptNhapXuatTon rptNXT = new rptNhapXuatTon();
                rptNhapXuatTonTheoMau rptNXT = new rptNhapXuatTonTheoMau();


                dsNhapXuatTon ds = new dsNhapXuatTon();
                DataRow row;
                for (int i = 0; i < lstTonKho.Count; i++)
                {

                    row = ds.Tables["dtNXT"].NewRow();
                    row["MaVTPT"] = lstTonKho[i].MaVTPT;
                    row["TenVTPT"] = lstTonKho[i].TenVTPT;
                    row["DauKy"] = lstTonKho[i].DauKy;
                    row["CuoiKy"] = lstTonKho[i].CuoiKy;
                    row["DonViTinh"] = lstTonKho[i].DonViTinh;
                    row["TongNhap"] = lstTonKho[i].Nhap;
                    row["TongXuat"] = lstTonKho[i].Xuat*(-1);
                    row["TrangThai"] = lstTonKho[i].TenTrangThai;
                    row["MaKho"] = lstTonKho[i].MaKho;
                    row["PartNo"] = lstTonKho[i].TenCacThuocTinh;


                    ds.Tables["dtNXT"].Rows.Add(row);
                }

                for (int i = 0; i < lstTonKhoCT.Count; i++)
                {

                    row = ds.Tables["dtPhatSinh"].NewRow();

                    row["MaVTPT"] = lstTonKhoCT[i].MaVTPT;
                    row["MaKho"] = lstTonKhoCT[i].MaKho;
                    row["TrangThai"] = lstTonKhoCT[i].TenTrangThai;

                    if (lstTonKhoCT[i].MaGiaoTacKho.Contains("NHAP"))
                    {
                        row["SLNhap"] = lstTonKhoCT[i].SoLuong;
                        row["NgayNhap"] = Convert.ToString("0" + lstTonKhoCT[i].Ngay.Day.ToString()).Substring(0, 2) + "-" + Convert.ToString("0" + lstTonKhoCT[i].Ngay.Month.ToString()).Substring(0, 2) + "-" + lstTonKhoCT[i].Ngay.Year.ToString();
                    }
                    else
                    {
                        row["SLXuat"] = lstTonKhoCT[i].SoLuong*(-1);
                        row["NgayXuat"] = Convert.ToString("0" + lstTonKhoCT[i].Ngay.Day.ToString()).Substring(0, 2) + "-" + Convert.ToString("0" + lstTonKhoCT[i].Ngay.Month.ToString()).Substring(0, 2) + "-" + lstTonKhoCT[i].Ngay.Year.ToString();
                    }

                    ds.Tables["dtPhatSinh"].Rows.Add(row);
                }  


                rptNXT.SetDataSource(ds);
                rptNXT.SetParameterValue("prMaDV", frmDangNhap.objTau.tenTau);
                rptNXT.SetParameterValue("prTuNgay", this.dateEditFromDate.DateTime.Day);
                rptNXT.SetParameterValue("prTuThang", this.dateEditFromDate.DateTime.Month);
                rptNXT.SetParameterValue("prTuNam", this.dateEditFromDate.DateTime.Year);
                rptNXT.SetParameterValue("prDenNgay", this.dateEditToDate.DateTime.Day);
                rptNXT.SetParameterValue("prDenThang", this.dateEditToDate.DateTime.Month);
                rptNXT.SetParameterValue("prDenNam", this.dateEditToDate.DateTime.Year);

               
                frmInBaoCao f = new frmInBaoCao(rptNXT,false);
                f.Show();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmdChon_Click(object sender, EventArgs e)
        {
            try
            {
                string makho = this.lookUpKho.Text == "" ? "" : this.lookUpKho.GetColumnValue("MaKho").ToString();
                if (makho != "")
                {
                    if (!this.cboKhoChon.Items.Contains(makho))
                    {
                        this.cboKhoChon.Items.Add(makho);
                        this.cboKhoChon.Refresh();
                    }
                }
            }
            catch { }
        }

        private void GetTonKhoTongHop()
        {
            try
            {
                lstTonKho.Clear();

                string makho = "";
                int matrangthai = 0;
                string manhomVTPT = "";
                DateTime fDate;
                DateTime tDate;

                fDate = this.dateEditFromDate.DateTime;
                tDate = this.dateEditToDate.DateTime;
                makho = this.lookUpKho.Text == "" ? "" : this.lookUpKho.GetColumnValue("MaKho").ToString();
                matrangthai = this.lookUpTrangthai.Text == "" ? -1 : int.Parse(this.lookUpTrangthai.GetColumnValue("MaTrangThai").ToString());
                manhomVTPT = this.lookUpNhomVTPT.Text == "" ? "" : (this.lookUpNhomVTPT.GetColumnValue("MaNhomVTPT").ToString());

                clsTON_KHO tkCtrl = new clsTON_KHO(_sConn);
                TON_KHO obj = new TON_KHO();
                clsTHUOC_TINH ttCtrl = new clsTHUOC_TINH(_sConn);
                List<THUOC_TINH> lstTT = new List<THUOC_TINH>();

                if (this.cboKhoChon.Items.Count == 0)
                {
                    obj.MaKho = makho;
                }
                
                obj.MaTrangThai = matrangthai;
                obj.FromDate = fDate;
                obj.ToDate = tDate;
                obj.NhomVTPTFrom = manhomVTPT;

                if (chkThietYeu.Checked == true)
                    obj.ThietYeu = 1;
                else
                    obj.ThietYeu = 2;

                if (lstVTPT.Count > 0)
                {
                    List<TON_KHO> lsttemp = new List<TON_KHO>();
                    for (int i = 0; i < lstVTPT.Count; i++)
                    {
                        obj.MaVTPT = lstVTPT[i]._maVTPT;
                        if (this.cboKhoChon.Items.Count > 0)
                        {
                            for (int k = 0; k < this.cboKhoChon.Items.Count; k++)
                            {
                                obj.MaKho = this.cboKhoChon.Items[k].ToString();
                                lsttemp.Clear();
                                lsttemp = tkCtrl.usp_XemTON_KHO_Tonghop(obj);

                                for (int j = 0; j < lsttemp.Count; j++)
                                {
                                    lstTT.Clear();
                                    lstTT = ttCtrl.SelectTHUOC_TINHByID(lsttemp[j].MaVTPT);
                                    foreach (THUOC_TINH t in lstTT)
                                    {
                                        if (Array.IndexOf(arrThamSo, t._maLoaiThuocTinh) >= 0)
                                        {
                                            if (t._giaTriTT != "" && t._giaTriTT != null)
                                            {
                                                lsttemp[j].TenCacThuocTinh += t._tenLoaiTT + ": " + t._giaTriTT + ";\n";
                                            }
                                        }
                                    }

                                    lstTonKho.Add(lsttemp[j]);
                                }
                            }
                        }
                        else
                        {
                            lsttemp.Clear();
                            lsttemp = tkCtrl.usp_XemTON_KHO_Tonghop(obj);

                            for (int j = 0; j < lsttemp.Count; j++)
                            {
                                lstTT.Clear();
                                lstTT = ttCtrl.SelectTHUOC_TINHByID(lsttemp[j].MaVTPT);
                                foreach (THUOC_TINH t in lstTT)
                                {
                                    if (Array.IndexOf(arrThamSo, t._maLoaiThuocTinh) >= 0)
                                    {
                                        if (t._giaTriTT != "" && t._giaTriTT != null)
                                        {
                                            lsttemp[j].TenCacThuocTinh += t._tenLoaiTT + ": " + t._giaTriTT + ";\n";
                                        }
                                    }
                                }
                              
                                lstTonKho.Add(lsttemp[j]);
                            }
                        }
                    }
                }
                else
                {
                    obj.MaVTPT = this.txtMaVTPT.Text;
                    List<TON_KHO> lsttemp = new List<TON_KHO>();
                    if (this.cboKhoChon.Items.Count > 0)
                    {
                        for (int k = 0; k < this.cboKhoChon.Items.Count; k++)
                        {
                            obj.MaKho = this.cboKhoChon.Items[k].ToString();
                            lsttemp.Clear();
                            lsttemp = tkCtrl.usp_XemTON_KHO_Tonghop(obj);
                            for (int j = 0; j < lsttemp.Count; j++)
                            {
                                lstTT.Clear();
                                lstTT = ttCtrl.SelectTHUOC_TINHByID(lsttemp[j].MaVTPT);
                                foreach (THUOC_TINH t in lstTT)
                                {
                                    if (Array.IndexOf(arrThamSo, t._maLoaiThuocTinh) >= 0)
                                    {
                                        if (t._giaTriTT != "" && t._giaTriTT != null)
                                        {
                                            lsttemp[j].TenCacThuocTinh += t._tenLoaiTT + ": " + t._giaTriTT + ";\n";
                                        }
                                    }
                                }

                                lstTonKho.Add(lsttemp[j]);
                            }
                        }
                    }
                    else
                    {
                        lsttemp.Clear();
                        lsttemp = tkCtrl.usp_XemTON_KHO_Tonghop(obj);

                        for (int j = 0; j < lsttemp.Count; j++)
                        {
                            lstTT.Clear();
                            lstTT = ttCtrl.SelectTHUOC_TINHByID(lsttemp[j].MaVTPT);
                            foreach (THUOC_TINH t in lstTT)
                            {
                                if (Array.IndexOf(arrThamSo, t._maLoaiThuocTinh) >= 0)
                                {
                                    if (t._giaTriTT != "" && t._giaTriTT != null)
                                    {
                                        lsttemp[j].TenCacThuocTinh += t._tenLoaiTT + ": " + t._giaTriTT + ";\n";
                                    }
                                }
                            }

                            lstTonKho.Add(lsttemp[j]);
                        }
                    }
                }
                this.gridControl1.RefreshDataSource();
            }
            catch { }
        }

        private void tìmTheoNhiềuTiêuChíToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstVTPT.Count == 0)
            {
                frm = new frmTimKiemVTPT(_sConn, true);
                frm.SelectLstMaterial += new frmTimKiemVTPT.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm.ShowDialog();
            
        }

        private void tìmTheoHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstVTPT.Count == 0)
            {
                frm1 = new frmTimKiemTheoHinh(_sConn, true);
                frm1.SelectLstMaterialByImg += new frmTimKiemTheoHinh.SelectListMaterialByImgHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm1.ShowDialog();
        }

        private void tìmVTIMPATheoNhiềuTiêuChíToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm2 == null)
            {
                frm2 = new frmImpa_TimKiem(_sConn, true);
                frm2.SelectLstMaterial += new frmImpa_TimKiem.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm2.ShowDialog();
        }

        private void tìmVTIMPATheoHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm3 == null)
            {
                frm3 = new frmImpa_TimKiemTheoHinh(_sConn, true);
                frm3.SelectLstMaterial += new frmImpa_TimKiemTheoHinh.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm3.ShowDialog();
        }

        private void gridDSVT_RowStyle(object sender, RowStyleEventArgs e)
        {
            GridView View = sender as GridView;
            if (e.RowHandle >= 0)
            {

                string ThietYeu = View.GetRowCellDisplayText(e.RowHandle, "ThietYeu").ToString().Trim();

                if (ThietYeu == "1")
                {
                    e.Appearance.ForeColor = Color.Red;
                    gridDSVT.OptionsSelection.EnableAppearanceFocusedRow = false;
                }
            }
        }

        private void txtMaVTPT_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    // kiểm tra tồn tại của VTPT                   

                    VAT_TU_PHU_TUNG obj = new VAT_TU_PHU_TUNG();

                    clsVAT_TU_PHU_TUNG vtptCtrl = new clsVAT_TU_PHU_TUNG(_sConn);

                    obj = vtptCtrl.SelectVAT_TU_PHU_TUNG(this.txtMaVTPT.Text.Trim(), frmDangNhap.objTau.maTau);
                    if (obj != null && obj._maVTPT != "" && obj._maVTPT != null)
                    {
                        this.lblTenVTPT.Text = obj._tenVTPT;
                       
                    }
                    else
                    {
                        MessageBox.Show("KHÔNG TÌM THẤY THÔNG TIN VẬT TƯ NÀY");
                       
                    }

                }
            }
            catch { }
        }
    }
}